﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Collections;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using Chenxin.Lib.Utility;

namespace Chenxin.Lib.House.BLL
{
    public class Customer_BaseInfo
    {
        //Cache服务类
        private Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.HOUSE);

        public DataSet getListByParams(Hashtable htParams)
        {
            int PageIndex = 1;
            int PageSize = 10;
            string OrderField = "Customer_BaseInfo.Adddate desc";
            string FieldList = "CustomerID,CustomerName,CustomerSex,CustomerKind,Kind,Nationality,Phone,Mobile,CertificateType,CertificateNo,Address,OprDepart,OprDepartId,OprPerson,PubLevel,LastTraceDay,PubLevel,Adddate";
            string TableFieldList = "CustomerID,CustomerName,(case sex when 20 then '女' else '男' end) as CustomerSex,CustomerKind, Nationality,Phone,Mobile,CertificateType,CertificateNo,Address,OprDepart,OprDepartId,OprPerson,Adddate,LastTraceDay,";
            TableFieldList = TableFieldList + "(Case PubLevel When 0 Then '私客' When 10 Then '公司级共享' When 20 Then '跨部级共享' When 30 Then '本部级共享' Else '' End) As PubLevel,";
            TableFieldList = TableFieldList + "(Case CustomerKind When 10 Then '房源客户' When 20 Then '需求客户' When 30 Then '带看客户' When 40 Then '成交客户' When 50 Then '委托客户' Else ''  End) As Kind";
        
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //私客户权限SQL
            System.Text.StringBuilder strSqlPriv = new System.Text.StringBuilder();
            //公客户权限SQL
            System.Text.StringBuilder strSqlPub = new System.Text.StringBuilder();
            string strPermit;

            //权限
            if (htParams.ContainsKey("Permit_Priv"))
            {
                strPermit = htParams["Permit_Priv"].ToString();
                //查看所有私客户权限
                if (strPermit == Constants.Permit_ViewAllPrivCustomer)
                {
                    strSqlPriv.Append(" (PubLevel = 0) ");
                }
                //查看城市私客户权限
                else if (strPermit == Constants.Permit_ViewCityPrivCustomer)
                {
                    strSqlPriv.AppendFormat(" (CityId = '{0}' And PubLevel = 0) ", htParams["CurCityId_Priv"]);   
                }
                //查看公司私客户权限
                else if (strPermit == Constants.Permit_ViewCompanyPrivCustomer)
                {
                    strSqlPriv.AppendFormat(" (CompanyId = '{0}' And PubLevel = 0) ", htParams["CurCompanyId_Priv"]);
                }
                //查看部门私客户权限
                else if (strPermit == Constants.Permit_ViewDepartPrivCustomer)
                {
                    strSqlPriv.Append(" EXISTS(");
                    strSqlPriv.Append("Select 1 from v_Struct_UserDepartRelation M ");
                    strSqlPriv.AppendFormat("WHERE Customer_BaseInfo.OprDepartId = M.Departid AND  M.Userid= '{0}' ", htParams["CurUserId_Priv"]);
                    strSqlPriv.Append("And Customer_BaseInfo.PubLevel = 0 AND M.ViewPrivCustomer =1) ");                
                }

            }
            if (htParams.ContainsKey("Permit_Pub"))
            {
                strPermit = htParams["Permit_Pub"].ToString();
                //查看所有公客户权限
                if (strPermit == Constants.Permit_ViewAllPubCustomer)
                {
                    strSqlPub.Append(" (PubLevel > 0) ");
                }
                //查看城市公客户权限
                else if (strPermit == Constants.Permit_ViewCityPubCustomer)
                {
                    strSqlPub.AppendFormat(" (CityId = '{0}' And PubLevel > 0) ", htParams["CurCityId_Pub"]);
                }
                //查看公司公客户权限
                else if (strPermit == Constants.Permit_ViewCompanyPubShareCustomer)
                {
                    strSqlPub.AppendFormat(" (CompanyId = '{0}' And PubLevel = 10) ", htParams["CurCompanyId_Pub"]);
                }
                //查看跨部门公客户权限
                else if (strPermit == Constants.Permit_ViewCrossDepartPubShareCustomer)
                {
                    strSqlPub.Append(" (EXISTS(");
                    strSqlPub.Append("Select 1 From v_Struct_UserDepartRelation M ");
                    strSqlPub.AppendFormat("WHERE Customer_BaseInfo.OprDepartId = M.Departid And M.Userid= '{0}' ", htParams["CurUserId_Pub"]);
                    strSqlPub.Append("And ((Customer_BaseInfo.PubLevel = 20 AND M.ViewCrossPubShareCustomer =1) ");
                    strSqlPub.Append("Or (Customer_BaseInfo.PubLevel = 30 AND M.ViewPubShareCustomer =1))) ");
                    strSqlPub.AppendFormat(" Or (PubLevel = 10 And CompanyId='{0}') OR (PubLevel > 0 And OprDepartId='{1}')) ", htParams["CurCompanyId_Pub"], htParams["CurDepartId_Pub"]);
                }
                //查看本部门公客户权限
                else if (strPermit == Constants.Permit_ViewDepartPubShareCustomer)
                {
                    strSqlPub.AppendFormat(" ((PubLevel = 10 And CompanyId='{0}') OR (PubLevel > 0 And OprDepartId='{1}')) ", htParams["CurCompanyId_Pub"], htParams["CurDepartId_Pub"]);
                }

            }
            if (strSqlPub.Length > 0 && strSqlPriv.Length > 0)
            {
                sb.AppendFormat(" ({0} Or {1} Or OprPersonId='{2}') ", strSqlPub, strSqlPriv, htParams["OprPersonId"]);
            }
            else if (strSqlPub.Length + strSqlPriv.Length > 0)
            {
                sb.AppendFormat(" (" + strSqlPriv.ToString() + strSqlPub.ToString() + " Or OprPersonId='{0}') ", htParams["OprPersonId"]);
            }
            else
            {
                sb.AppendFormat(" (OprPersonId='{0}') ", htParams["OprPersonId"]);
            }
               
            //else
            //{
            //    sb.AppendFormat(" (OprPersonId='{0}') ", htParams["CurUserId_Pub"]);
            //}

            //用户名
            if (htParams.ContainsKey("UserId"))
            {
                sb.Append(" And OprPersonId = '" + htParams["UserId"] + "'");
            }
            //部门
            else if (htParams.ContainsKey("DepartId"))
            {
                sb.Append(" And OprDepartId = '" + htParams["DepartId"] + "'");
            }
            //商圈
            else if (htParams.ContainsKey("AreaId"))
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo entArea = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, htParams["AreaId"]);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);

                entArea = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    entArea = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(new Guid(htParams["AreaId"].ToString()));
                    if (entArea != null) cs.Set(strCachkey, entArea, DateTime.Now.AddDays(1));
                }

                if (entArea != null)
                {
                    sb.Append(" And EXISTS(");
                    sb.AppendFormat("Select 1 from v_Hr_DepartmentInfo M WHERE Customer_BaseInfo.OprDepartId=M.DepartId And M.DepartSrc like '{0}%'", entArea.DepartSrc);
                    sb.Append(") ");
                }
            }
            //公司
            else if (htParams.ContainsKey("CompanyId"))
            {
                sb.AppendFormat(" And CompanyId = '{0}'", htParams["CompanyId"]);
            }
            //城市
            else if (htParams.ContainsKey("CityId"))
            {
                sb.AppendFormat(" And CityId = '{0}'", htParams["CityId"]);
            }

            //客户姓名
            if (htParams.ContainsKey("CustomerName"))
            {
                sb.AppendFormat(" And CustomerName like '%{0}%'", htParams["CustomerName"].ToString());
            }
            //客户类别
            if (htParams.ContainsKey("CustomerKind"))
            {
                sb.AppendFormat(" And CustomerKind = {0} ", htParams["CustomerKind"]);
            }
            //客户电话
            if (htParams.ContainsKey("txtcustomerphone"))
            {
                sb.AppendFormat(" And (Mobile LIKE '%{0}%' OR Phone Like '%{0}%') ", htParams["txtcustomerphone"]);
            }
            //客户公开级别
            if (htParams.ContainsKey("CustomerPubLevel"))
            {
                sb.AppendFormat(" And PubLevel = {0} ", htParams["CustomerPubLevel"]);
            }
            if (htParams.ContainsKey("PageIndex"))
            {
                PageIndex = Convert.ToInt32(htParams["PageIndex"]) + 1;
            }

            if (htParams.ContainsKey("PageSize"))
            {
                PageSize = Convert.ToInt32(htParams["PageSize"]);
            }

            return Chenxin.Lib.House.DAL.Customer_BaseInfo.GetList(PageIndex, PageSize, OrderField, sb.ToString(), FieldList, TableFieldList, true);
        }

        public DataSet getList(Hashtable htParams,string Sort)
        {
            int PageIndex = 1;
            int PageSize = 10;
            string OrderField = Sort;
            string FieldList = "CustomerID,CustomerName,CustomerSex,CustomerKind,Kind,Nationality,Phone,Mobile,CertificateType,CertificateNo,Address,OprDepart,OprDepartId,OprPerson,PubLevel,LastTraceDay,PubLevel,Adddate";
            string TableFieldList = "CustomerID,CustomerName,(case sex when 20 then '女' else '男' end) as CustomerSex,CustomerKind, Nationality,Phone,Mobile,CertificateType,CertificateNo,Address,OprDepart,OprDepartId,OprPerson,Adddate,LastTraceDay,";
            TableFieldList = TableFieldList + "(Case PubLevel When 0 Then '私客' When 10 Then '公司级共享' When 20 Then '跨部级共享' When 30 Then '本部级共享' Else '' End) As PubLevel,";
            TableFieldList = TableFieldList + "(Case CustomerKind When 10 Then '房源客户' When 20 Then '需求客户' When 30 Then '带看客户' When 40 Then '成交客户' When 50 Then '委托客户' Else ''  End) As Kind";

            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            //私客户权限SQL
            System.Text.StringBuilder strSqlPriv = new System.Text.StringBuilder();
            //公客户权限SQL
            System.Text.StringBuilder strSqlPub = new System.Text.StringBuilder();
            string strPermit;

            //权限
            if (htParams.ContainsKey("Permit_Priv"))
            {
                strPermit = htParams["Permit_Priv"].ToString();
                //查看所有私客户权限
                if (strPermit == Constants.Permit_ViewAllPrivCustomer)
                {
                    strSqlPriv.Append(" (PubLevel = 0) ");
                }
                //查看城市私客户权限
                else if (strPermit == Constants.Permit_ViewCityPrivCustomer)
                {
                    strSqlPriv.AppendFormat(" (CityId = '{0}' And PubLevel = 0) ", htParams["CurCityId_Priv"]);
                }
                //查看公司私客户权限
                else if (strPermit == Constants.Permit_ViewCompanyPrivCustomer)
                {
                    strSqlPriv.AppendFormat(" (CompanyId = '{0}' And PubLevel = 0) ", htParams["CurCompanyId_Priv"]);
                }
                //查看部门私客户权限
                else if (strPermit == Constants.Permit_ViewDepartPrivCustomer)
                {
                    strSqlPriv.Append(" EXISTS(");
                    strSqlPriv.Append("Select 1 from v_Struct_UserDepartRelation M ");
                    strSqlPriv.AppendFormat("WHERE Customer_BaseInfo.OprDepartId = M.Departid AND  M.Userid= '{0}' ", htParams["CurUserId_Priv"]);
                    strSqlPriv.Append("And Customer_BaseInfo.PubLevel = 0 AND M.ViewPrivCustomer =1) ");
                }

            }
            if (htParams.ContainsKey("Permit_Pub"))
            {
                strPermit = htParams["Permit_Pub"].ToString();
                //查看所有公客户权限
                if (strPermit == Constants.Permit_ViewAllPubCustomer)
                {
                    strSqlPub.Append(" (PubLevel > 0) ");
                }
                //查看城市公客户权限
                else if (strPermit == Constants.Permit_ViewCityPubCustomer)
                {
                    strSqlPub.AppendFormat(" (CityId = '{0}' And PubLevel > 0) ", htParams["CurCityId_Pub"]);
                }
                //查看公司公客户权限
                else if (strPermit == Constants.Permit_ViewCompanyPubShareCustomer)
                {
                    strSqlPub.AppendFormat(" (CompanyId = '{0}' And PubLevel = 10) ", htParams["CurCompanyId_Pub"]);
                }
                //查看跨部门公客户权限
                else if (strPermit == Constants.Permit_ViewCrossDepartPubShareCustomer)
                {
                    strSqlPub.Append(" (EXISTS(");
                    strSqlPub.Append("Select 1 From v_Struct_UserDepartRelation M ");
                    strSqlPub.AppendFormat("WHERE Customer_BaseInfo.OprDepartId = M.Departid And M.Userid= '{0}' ", htParams["CurUserId_Pub"]);
                    strSqlPub.Append("And ((Customer_BaseInfo.PubLevel = 20 AND M.ViewCrossPubShareCustomer =1) ");
                    strSqlPub.Append("Or (Customer_BaseInfo.PubLevel = 30 AND M.ViewPubShareCustomer =1))) ");
                    strSqlPub.AppendFormat(" Or (PubLevel = 10 And CompanyId='{0}') OR (PubLevel > 0 And OprDepartId='{1}')) ", htParams["CurCompanyId_Pub"], htParams["CurDepartId_Pub"]);
                }
                //查看本部门公客户权限
                else if (strPermit == Constants.Permit_ViewDepartPubShareCustomer)
                {
                    strSqlPub.AppendFormat(" ((PubLevel = 10 And CompanyId='{0}') OR (PubLevel > 0 And OprDepartId='{1}')) ", htParams["CurCompanyId_Pub"], htParams["CurDepartId_Pub"]);
                }

            }
            if (strSqlPub.Length > 0 && strSqlPriv.Length > 0)
            {
                sb.AppendFormat(" ({0} Or {1} Or OprPersonId='{2}') ", strSqlPub, strSqlPriv, htParams["OprPersonId"]);
            }
            else if (strSqlPub.Length + strSqlPriv.Length > 0)
            {
                sb.AppendFormat(" (" + strSqlPriv.ToString() + strSqlPub.ToString() + " Or OprPersonId='{0}') ", htParams["OprPersonId"]);
            }
            else
            {
                sb.AppendFormat(" (OprPersonId='{0}') ", htParams["OprPersonId"]);
            }

            //else
            //{
            //    sb.AppendFormat(" (OprPersonId='{0}') ", htParams["CurUserId_Pub"]);
            //}

            //用户名
            if (htParams.ContainsKey("UserId"))
            {
                sb.Append(" And OprPersonId = '" + htParams["UserId"] + "'");
            }
            //部门
            else if (htParams.ContainsKey("DepartId"))
            {
                sb.Append(" And OprDepartId = '" + htParams["DepartId"] + "'");
            }
            //商圈
            else if (htParams.ContainsKey("AreaId"))
            {
                Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo entArea = null;
                string strCachkey = string.Format(Chenxin.Lib.Cache.CacheDataType.GLOBAL_DEPARTMENT, htParams["AreaId"]);
                Chenxin.Lib.Cache.CacheService cs = new Chenxin.Lib.Cache.CacheService(Chenxin.Lib.Cache.CacheConn.xCacheConn.GLOBAL);

                entArea = cs.GetObject<Chenxin.Lib.Hr.Entity.Hr_DepartmentInfo>(strCachkey);
                if (!cs.Hit)
                {
                    entArea = Chenxin.Lib.Hr.DAL.Hr_DepartmentInfo.GetModel(new Guid(htParams["AreaId"].ToString()));
                    if (entArea != null) cs.Set(strCachkey, entArea, DateTime.Now.AddDays(1));
                }

                if (entArea != null)
                {
                    sb.Append(" And EXISTS(");
                    sb.AppendFormat("Select 1 from v_Hr_DepartmentInfo M WHERE Customer_BaseInfo.OprDepartId=M.DepartId And M.DepartSrc like '{0}%'", entArea.DepartSrc);
                    sb.Append(") ");
                }
            }
            //公司
            else if (htParams.ContainsKey("CompanyId"))
            {
                sb.AppendFormat(" And CompanyId = '{0}'", htParams["CompanyId"]);
            }
            //城市
            else if (htParams.ContainsKey("CityId"))
            {
                sb.AppendFormat(" And CityId = '{0}'", htParams["CityId"]);
            }

            //客户姓名
            if (htParams.ContainsKey("CustomerName"))
            {
                sb.AppendFormat(" And CustomerName like '%{0}%'", htParams["CustomerName"].ToString());
            }
            //客户类别
            if (htParams.ContainsKey("CustomerKind"))
            {
                sb.AppendFormat(" And CustomerKind = {0} ", htParams["CustomerKind"]);
            }
            //客户电话
            if (htParams.ContainsKey("txtcustomerphone"))
            {
                sb.AppendFormat(" And (Mobile LIKE '%{0}%' OR Phone Like '%{0}%') ", htParams["txtcustomerphone"]);
            }
            //客户公开级别
            if (htParams.ContainsKey("CustomerPubLevel"))
            {
                sb.AppendFormat(" And PubLevel = {0} ", htParams["CustomerPubLevel"]);
            }
            if (htParams.ContainsKey("PageIndex"))
            {
                PageIndex = Convert.ToInt32(htParams["PageIndex"]) + 1;
            }

            if (htParams.ContainsKey("PageSize"))
            {
                PageSize = Convert.ToInt32(htParams["PageSize"]);
            }

            return Chenxin.Lib.House.DAL.Customer_BaseInfo.GetList(PageIndex, PageSize, OrderField, sb.ToString(), FieldList, TableFieldList, true);
        }

        //*******************************************
        //功能概要       ：获取当前用户所属客户数量
        //参数説明       ：OprPersonId：客户Id
        //返回值説明     ：当前用户所属客户数量
        //功能詳細       ：获取当前用户所属客户数量
        //作成者         ：何基锋
        //作成日         ：2010-06-23
        //*******************************************
        public int GetCntOfCustomer(Guid OprPersonId)
        {
            return Chenxin.Lib.House.DAL.Customer_BaseInfo.GetCntOfCustomer(OprPersonId);
        }

        //客需对象
        public Chenxin.Lib.House.Entity.Customer_BaseInfo GetModel(Guid CustomerId)
        {
            return Chenxin.Lib.House.DAL.Customer_BaseInfo.GetModel(CustomerId);
        }

        //更新客户基本信息
        public void Update(Chenxin.Lib.House.Entity.Customer_BaseInfo model)
        {
            Chenxin.Lib.House.DAL.Customer_BaseInfo.Update(model);
        }
    }
}
